package com.xhwl.data.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.pojo.cent.sdata.Organization;
import com.xhwl.common.pojo.cent.sdata.personnel.Staff;
import com.xhwl.common.pojo.cent.sdata.personnel.StaffOrganization;
import com.xhwl.data.pojo.OrganizationStaffCount;
import com.xhwl.data.pojo.dto.personal.StaffOrganizationDTO;
import com.xhwl.data.service.IOrganizationService;
import com.xhwl.data.service.personal.IStaffOrganizationService;
import com.xhwl.data.service.personal.IStaffService;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: luyi
 * @Description: 调整组织
 * @Date: Create in 15:51 2022/2/8
 */
@RestController
public class StaffOrganizationController {

    @Autowired
    private IOrganizationService organizationService;

    @Autowired
    private IStaffOrganizationService staffOrganizationService;

    @Autowired
    private IStaffService staffService;

    private static final Logger log = LoggerFactory.getLogger(StaffOrganizationController.class);

    /**
     * 调整组织
     * @param staffOrganizationDTO
     * @return
     */
    @RequestMapping(value = "/staff-org/edit")
    public ResultJson delete(@RequestBody StaffOrganizationDTO staffOrganizationDTO){
        if(null == staffOrganizationDTO.getStaffId()){
            return ResultJson.fail("缺少员工信息");
        }
        if(CollectionUtils.isEmpty(staffOrganizationDTO.getOrgIds())){
            return ResultJson.fail("缺少组织信息");
        }
        QueryWrapper<Staff> staffQueryWrapper = new QueryWrapper<>();
        staffQueryWrapper.eq("id", staffOrganizationDTO.getStaffId());
        List<Staff> staff = staffService.list(staffQueryWrapper);
        if(staff.isEmpty()){
            return ResultJson.fail("员工不存在");
        }


        QueryWrapper<Organization> organizationQueryWrapper = new QueryWrapper<>();
        organizationQueryWrapper.select("id", "enterprise_id");
        List<Integer> orgIds = staffOrganizationDTO.getOrgIds().stream().distinct().collect(Collectors.toList());
        organizationQueryWrapper.in("id", orgIds);
        // 新添加的组织
        List<Organization> list = organizationService.list(organizationQueryWrapper);
        if(list.isEmpty()){
            return ResultJson.fail("组织不存在");
        }

        // 当前操作的企业
        Integer enterpriseId = list.get(0).getEnterpriseId();

        List<Integer> newOrgIds = list.stream().map(i -> i.getId()).collect(Collectors.toList());

        // 该员工之前绑定的组织
        QueryWrapper<StaffOrganization> staffOrganizationQueryWrapper = new QueryWrapper<>();
        staffOrganizationQueryWrapper.eq("enterprise_id", enterpriseId);
        staffOrganizationQueryWrapper.eq("staff_id", staffOrganizationDTO.getStaffId());
        List<StaffOrganization> originalOrgs = staffOrganizationService.list(staffOrganizationQueryWrapper);
        if(originalOrgs.isEmpty()){
            return ResultJson.fail("数据错误，该员工暂稳绑定组织。");
        }
        List<Integer> originalOrgIds = originalOrgs.stream().map(i -> i.getOrgId()).collect(Collectors.toList());
        List<Integer> bakOrgIds = originalOrgIds;
        // 取交集（不需要修改组织）
        originalOrgIds.retainAll(newOrgIds);

        // 需要删除的旧组织
        bakOrgIds.removeAll(originalOrgIds);
        QueryWrapper<StaffOrganization> staffOrganizationQueryWrapper1 = new QueryWrapper<>();
        staffOrganizationQueryWrapper1.eq("staff_id", staffOrganizationDTO.getStaffId());
        staffOrganizationQueryWrapper1.in("org_id", bakOrgIds);
        staffOrganizationService.remove(staffOrganizationQueryWrapper1);
        // 需要新增的新组织
        newOrgIds.removeAll(originalOrgIds);
        List<StaffOrganization> newList = new ArrayList<>(newOrgIds.size());
        for (Integer newOrgId : newOrgIds) {
            StaffOrganization staffOrganization = new StaffOrganization();
            staffOrganization.setOrgId(newOrgId);
            staffOrganization.setStaffId(staffOrganizationDTO.getStaffId());
            staffOrganization.setAccountId(originalOrgs.get(0).getAccountId());
            newList.add(staffOrganization);
        }
        staffOrganizationService.saveBatch(newList);
        return ResultJson.success();
    }

    @RequestMapping(value = "/staff-org/count")
    public ResultJson count(@RequestBody List<Integer> organizationIds){
        List<OrganizationStaffCount> organizationAccountCounts = staffOrganizationService.countOrg(organizationIds);
        return ResultJson.success(organizationAccountCounts);
    }
}
